package cn.stylefeng.guns.modular.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.oa.entity.Task;
import cn.stylefeng.guns.modular.oa.enums.TaskExceptionEnum;
import cn.stylefeng.guns.modular.oa.mapper.TaskMapper;
import cn.stylefeng.guns.modular.oa.pojo.request.TaskRequest;
import cn.stylefeng.guns.modular.oa.service.TaskService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 业务实现层
 *
 * @author 王鑫
 * @date 2024/08/07 17:27
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

	@Override
    public void add(TaskRequest taskRequest) {
        Task task = new Task();
        BeanUtil.copyProperties(taskRequest, task);
        this.save(task);
    }

    @Override
    public void del(TaskRequest taskRequest) {
        Task task = this.queryTask(taskRequest);
        this.removeById(task.getId());
    }

    @Override
    public void edit(TaskRequest taskRequest) {
        Task task = this.queryTask(taskRequest);
        BeanUtil.copyProperties(taskRequest, task);
        this.updateById(task);
    }

    @Override
    public Task detail(TaskRequest taskRequest) {
        return this.queryTask(taskRequest);
    }

    @Override
    public PageResult<Task> findPage(TaskRequest taskRequest) {
        LambdaQueryWrapper<Task> wrapper = createWrapper(taskRequest);
        Page<Task> task = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(task);
    }

    @Override
    public List<Task> findList(TaskRequest taskRequest) {
        LambdaQueryWrapper<Task> wrapper = this.createWrapper(taskRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 王鑫
     * @date 2024/08/07 17:27
     */
    private Task queryTask(TaskRequest taskRequest) {
        Task task = this.getById(taskRequest.getId());
        if (ObjectUtil.isEmpty(task)) {
            throw new ServiceException(TaskExceptionEnum.TASK_NOT_EXISTED);
        }
        return task;
    }

    /**
     * 创建查询wrapper
     *
     * @author 王鑫
     * @date 2024/08/07 17:27
     */
    private LambdaQueryWrapper<Task> createWrapper(TaskRequest taskRequest) {
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();

        Long id = taskRequest.getId();
        String taskNumber = taskRequest.getTaskNumber();
        Integer taskType = taskRequest.getTaskType();
        Integer taskStatus = taskRequest.getTaskStatus();
        Integer priority = taskRequest.getPriority();
        String submitter = taskRequest.getSubmitter();
        String processor = taskRequest.getProcessor();
        String description = taskRequest.getDescription();
        String deadline = taskRequest.getDeadline();
        String remarks = taskRequest.getRemarks();
        String attachments = taskRequest.getAttachments();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Task::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(taskNumber), Task::getTaskNumber, taskNumber);
        queryWrapper.eq(ObjectUtil.isNotNull(taskType), Task::getTaskType, taskType);
        queryWrapper.eq(ObjectUtil.isNotNull(taskStatus), Task::getTaskStatus, taskStatus);
        queryWrapper.eq(ObjectUtil.isNotNull(priority), Task::getPriority, priority);
        queryWrapper.like(ObjectUtil.isNotEmpty(submitter), Task::getSubmitter, submitter);
        queryWrapper.like(ObjectUtil.isNotEmpty(processor), Task::getProcessor, processor);
        queryWrapper.like(ObjectUtil.isNotEmpty(description), Task::getDescription, description);
        queryWrapper.like(ObjectUtil.isNotEmpty(deadline), Task::getDeadline, deadline);
        queryWrapper.like(ObjectUtil.isNotEmpty(remarks), Task::getRemarks, remarks);
        queryWrapper.like(ObjectUtil.isNotEmpty(attachments), Task::getAttachments, attachments);


        return queryWrapper;
    }

}
